#!/bin/bash
# echo the call so we can see what's going on
echo "$0 $1 $2 $3 $4 $5 $6"

# Check syntax
if [ "$1" = "" ]; then
	echo "$0 called without number of phase for which lattice should be varied or fixed!"
	exit 0
fi

if [ "$2" != "y" ]; then
	if [ "$2" != "n" ]; then
		echo "$0 called without proper flag (y/n) for lattice variation!"
		exit 0
	fi
fi

echo "k l o l" > temp.txt
echo "p $1" >> temp.txt
echo "v" >> temp.txt
echo "x" >> temp.txt
echo "x" >> temp.txt
echo "x" >> temp.txt
echo "x" >> temp.txt

# find out what the current flag is
grep "CRS$1  ABC   " `cat GSAS_EXP`.EXP | awk '{print $6}' > flag.txt    
echo "The refinement flag is <`cat flag.txt`>"
if [ "`cat flag.txt`" = " " ]; then
	echo "Could not determine refinement flag for lattice parameter of phase $1, trying again."
	grep "CRS$1  ABC   " `cat GSAS_EXP`.EXP | tail -c35 | head -c1 > flag.txt
	echo "The refinement flag is <`cat flag.txt`>"
fi

if [ "`cat flag.txt`" = "Y" ]; then
	# the refinement flag is turned on
	if [ "$2" = "y" ]; then
		# which is what we want, no action
		echo "Lattice for phase $1 is already varying!"
	else
		# it is on, but we want it off, let's toggle it
		echo "Lattice for phase $1 is fixed, will toggle!"
		expedt `cat GSAS_EXP` < temp.txt > out.txt
	fi
else
	# flag is either not set (off) or set to "N"
	if [ "$2" = "y" ]; then
		# it is off, but we want it on, let's toggle it
		echo "Lattice for phase $1 is varying, let's toggle!"
		expedt `cat GSAS_EXP` < temp.txt > out.txt
	else
		# which is what we want, no action
		echo "Lattice for phase $1 is already fixed!"
	fi
fi

# Test the EXP file
grep "CRS$1  ABC   " `cat GSAS_EXP`.EXP | tail -c36 | head -c1 > flag.txt
echo "The refinement flag is <`cat flag.txt`>"
if [ "`cat flag.txt`" = " " ]; then
	echo "Could not determine refinement flag for lattice parameter of phase $1, trying again."
	grep "CRS$1  ABC   " `cat GSAS_EXP`.EXP | tail -c35 | head -c1 > flag.txt
	echo "The refinement flag is <`cat flag.txt`>"
fi

if [ "`cat flag.txt`" = "Y" ]; then
	# the refinement flag is turned on
	if [ "$2" = "y" ]; then
		# which is what we want, no action
		echo "Lattice for phase $1 is varying as intended!"
	else
		# it is on, but we want it off, let's toggle it
		echo "Problem! The flag is not set to what was desired!"
		read -p "Maybe you want to debug?"
	fi
else
	# flag is either not set (off) or set to "N"
	if [ "$2" = "y" ]; then
		# it is off, but we want it on, let's toggle it
		echo "Problem! The flag is not set to what was desired!"
		read -p "Maybe you want to debug?"
	else
		# which is what we want, no action
		echo "Lattice for phase $1 is now fixed!"
	fi
fi

if [ "$3" != " " ]; then
	# we have a damping flag, set it
	echo "k l o l" > temp.txt
	echo "p $1" >> temp.txt
	echo "d $3" >> temp.txt
	echo "x" >> temp.txt
	echo "x" >> temp.txt
	echo "x" >> temp.txt
	echo "x" >> temp.txt
	expedt `cat GSAS_EXP` < temp.txt > out.txt
fi

echo "Changed lattice parameter refinement flag for phase $1 to $2" >> temp.tex
if [ "$3" != " " ]; then
	echo "(damping set to $3)" >> temp.tex
fi
echo "." >> temp.tex


